package com.chenxy.user.center.controller;

import com.chenxy.common.controller.BaseController;
import io.swagger.annotations.Api;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.boot.autoconfigure.klock.model.LockTimeoutStrategy;
import org.springframework.boot.autoconfigure.klock.model.LockType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Api(tags = "测试使用")
@RestController
@RequestMapping("test")
public class TestController extends BaseController{

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/test1")
    public String test1(){
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String ss = dateTimeFormatter.format(localDateTime).toString();
        return ss;
    }

    @GetMapping("/test3")
    @Klock(keys = {"#userid"},waitTime = 0,lockType= LockType.Write,lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
    public String test3(String userid,int id){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "success";
    }

    /*@GetMapping("/test4")
    public Res test4(){
        UserDO userDO = new UserDO();
        userDO.setId(1);
        userDO.setUsername("hhh");
        redisTemplate.opsForValue().set("test:test3",userDO);
        stringRedisTemplate.opsForValue().set("test:test4","test4");
        return Res.success(null);
    }*/

}
